package com.bw.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bw.dto.Goods;
import com.bw.dto.User;
import com.bw.dto.Usergoods;
import com.bw.service.UserService;

@Controller
public class UserCon {

	@Autowired
	private UserService userService;
	
	@RequestMapping(value="deng")
	public void deng(User user,HttpServletResponse response,HttpSession session) throws IOException{
		
		Boolean b=null;
		
		b=userService.dodeng(user);
		List<User> list=userService.dogetlist();
		
		for (User li : list) {
			int money = li.getMoney();
			session.setAttribute("money", money);
		}
		
		String username = user.getUsername();
		session.setAttribute("username", username);
		if(b!=null){
			if(b){
				response.getWriter().print(true);
			}else{
				response.getWriter().print(false);
			}
		}
	}
	
	@RequestMapping(value="list")
	public String list(HttpServletRequest request){
		List list=userService.dolist();
		request.setAttribute("list", list);
		return "list";
	}
	@RequestMapping(value="gou")
	public String gou(int gid,HttpServletRequest request){
		Goods goods=userService.add(gid);
		request.setAttribute("goods", goods);
		return "add";
	}
	@RequestMapping(value="goumai")
	public void goumai(Usergoods usergoods,HttpServletResponse response,HttpSession session) throws IOException{
		
		int count = usergoods .getCount();
		System.out.println(count);
		int a=0;
		
		a=3*count;
		System.out.println(a);
		List<User> list=userService.dogetlist();
		
		for (User li : list) {
			int money = li.getMoney()-a;
			System.out.println(money);
			session.setAttribute("money", money);
		}
		
		
		int b=userService.doadd(usergoods);
		if(b>0){
			response.getWriter().println("1");
		}else{
			response.getWriter().print("0");
		}
	}
}
